package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.MultilevelActivityBond;
import com.x.erp.rpc.MultilevelActivityBondRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多级营销活动保证金信息
 */
@RestController
@RequestMapping("/multilevelActivityBond")
public class MultilevelActivityBondController extends BaseController{

	@Reference
	private MultilevelActivityBondRpcService multilevelActivityBondRpcService;// 多级营销活动保证金信息服务

	/**
	 * 查询多级营销活动保证金信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityBondList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBondList(MultilevelActivityBond multilevelActivityBond) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityBondRpcService.selectMultilevelActivityBondList(multilevelActivityBond));
	}

	/**
	 * 查询多级营销活动保证金信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityBondNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBondNum(MultilevelActivityBond multilevelActivityBond) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", multilevelActivityBondRpcService.selectMultilevelActivityBondNum(multilevelActivityBond));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询多级营销活动保证金信息
	 * @param
	 * @return
			 */
	@RequestMapping(value = "/getMultilevelActivityBond", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBond(MultilevelActivityBond multilevelActivityBond) {
		if(multilevelActivityBond == null || StringUtils.isEmpty(multilevelActivityBond.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车商ID不能为空！");
		}
		//查询app当前版本
		List<MultilevelActivityBond> multilevelActivityBonds = multilevelActivityBondRpcService.selectMultilevelActivityBondList(multilevelActivityBond);
		if(multilevelActivityBonds != null && multilevelActivityBonds.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityBonds.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增多级营销活动保证金信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertMultilevelActivityBond", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertMultilevelActivityBond(MultilevelActivityBond multilevelActivityBond) {
		if(multilevelActivityBond == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销活动保证金信息不能为空！");
		}
		if(multilevelActivityBond.getBondMoney() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "保证金金额不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityBond.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车商ID不能为空！");
		}
		MultilevelActivityBond bond = new MultilevelActivityBond();
		bond.setShopId(multilevelActivityBond.getShopId());
		if(!multilevelActivityBondRpcService.selectMultilevelActivityBondList(bond).isEmpty()){
			return new ResultModel(ResultCode.RESULT_FAIL, "该车商已存在！");
		}
		Date cDate = this.getDate(0);
		multilevelActivityBond.setBondFreezingMoney(new BigDecimal(0));
		multilevelActivityBond.setBondCreateTime(cDate);
		multilevelActivityBond.setBondUpdateTime(cDate);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", multilevelActivityBondRpcService.insertMultilevelActivityBond(multilevelActivityBond));
	}

}
